package com.dg.river.core.http;

import com.dg.river.contant.Constant;
import com.dg.river.core.util.L;
import com.dg.river.core.util.TextUtil;
import com.dg.river.module.base.bean.BaseBean;
import com.google.gson.Gson;

/**
 * @作者：Hututu
 * @时间：2022/6/1 15:58
 * @描述：内容描述
 * @修改：修改内容
 * @Version:1.0
 */
public class BeanUtils {

    private static BeanUtils beanUtils;

    public static BeanUtils getInstance() {
        if (beanUtils == null) {
            synchronized (BeanUtils.class) {
                if (beanUtils == null) {
                    beanUtils = new BeanUtils();
                }
            }
        }
        return beanUtils;
    }

    /**
     * 转换Bean，拦截异常
     *
     * @param json
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> T convertBean(String json, Class<T> tClass, BeanCode beanCode) {
        try {
            return new Gson().fromJson(json, tClass);
        } catch (Exception e) {
            L.d("数据异常，请联系管理员。【 code：" + beanCode.value + " 】");
            L.d("数据异常，请联系管理员。" + e.toString());
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> boolean checkSuccess(T bean) {
        if (bean != null && bean.getCode() != null && Constant.RESULT_SUCCESS.equals(bean.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> boolean checkFailure10007(T bean) {
        if (bean != null && bean.getCode() != null && Constant.RESULT_ERROR_10007.equals(bean.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> boolean checkFailure10002(T bean) {
        if (bean != null && bean.getCode() != null && Constant.RESULT_ERROR_10002.equals(bean.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> boolean checkFailure10008(T bean) {
        if (bean != null && bean.getCode() != null && Constant.RESULT_ERROR_10008.equals(bean.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> boolean checkFailure100301(T bean) {
        if (bean != null && bean.getCode() != null && Constant.RESULT_ERROR_100301.equals(bean.getCode())) {
            return true;
        }
        return false;
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> String toastMsg(T bean) {
        if (bean != null) {
            return TextUtil.isEmptyConvert(bean.getMsg());
        } else {
            return "";
        }
    }

    /**
     * 校验返回值是否正确
     *
     * @param bean
     * @param <T>
     * @return
     */
    public <T extends BaseBean> String toastMsg(T bean, String defaultMsg) {
        if (bean != null) {
            return TextUtil.isEmptyConvert(bean.getMsg());
        } else {
            return defaultMsg;
        }
    }
}
